<template>
  <div>
    <!-- 结算单查看 dialog -->
    <el-dialog
      :title="info.title"
      :visible.sync="info.show"
      :close-on-click-modal="false"
      v-el-drag-dialog
      v-dialogDrag
      @open="dialogOpen"
      @close="dialogClose"
      width="1500px"
    >
      <div>
        <div>
          <el-form ref="payInfoRef" :model="payInfo" size="mini" :inline="true" label-position="right" class="view-form">
            <!-- <el-row class="dialog-row" style="margin-top:20px;">
              <el-col> -->
            <div style="margin-top:20px;margin-left:20px">
              <el-form-item label="结算对象:" prop="customerName" size="mini">
                <el-input v-model="payInfo.customer" disabled size="mini" style="width:150px"></el-input>
              </el-form-item>
              <!-- </el-col>
              <el-col> -->
              <el-form-item label="结算单号:" prop="customerName" size="mini">
                <el-input v-model="payInfo.billNo" disabled size="mini" style="width:150px"></el-input>
              </el-form-item>
              <!-- </el-col>
              <el-col> -->
              <el-form-item label="结算单名称:" prop="customerName" size="mini">
                <el-input v-model="payInfo.billName" disabled size="mini" style="width:150px"></el-input>
              </el-form-item>
              <!-- </el-col> -->
              <!-- </el-row>
              <el-row class="dialog-row">-->
              <!-- <el-col> -->
              <el-form-item label="结算单合计:" prop="customerName" size="mini">
                <el-input v-model="payInfo.billTotal" disabled size="mini" style="width:150px"></el-input>
              </el-form-item>
              <!-- </el-col> -->
              <!-- </el-row> -->
              <el-form-item label="价税合计:" prop="taxCount" size="mini">
                <el-input v-model="payInfo.taxCount" disabled size="mini" style="width: 150px"></el-input>
              </el-form-item>
              <el-row>
                <el-button v-if="!info.documentNoDisable" size="mini" style="margin:10px;" type="primary" @click="addDocumentList">添加</el-button>
              </el-row>
            </div>
            <el-table :data="payInfo.documentList" :header-cell-style="{ background: '#eef1f6', color: '#606266' }" border style="width: 100%;" width="100%">
              <el-table-column align="center" label="序号" type="index" width="55"></el-table-column>
              <el-table-column align="center" label="发票编号">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentNo'"
                    :rules="[{ required: true, message: '发票编号不能为空', trigger: 'blur' }]"
                  >
                    <el-input :disabled="info.documentNoDisable" placeholder="请输入" size="mini" v-model="payInfo.documentList[scope.$index].cbdDocumentNo" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="发票代码">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentCode'"
                    :rules="[{ required: true, message: '请输入发票代码', trigger: 'blur' }]"
                  >
                    <el-input
                      clearable
                      :disabled="info.documentNoDisable"
                      maxlength="32"
                      placeholder="请输入"
                      size="mini"
                      v-model="payInfo.documentList[scope.$index].cbdDocumentCode"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="对方纳税人识别号" width="150">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentIdentifier'"
                    :rules="[{ required: true, message: '请输入对方纳税人识别号', trigger: 'blur' }]"
                  >
                    <el-input
                      clearable
                      :disabled="info.documentNoDisable"
                      maxlength="32"
                      placeholder="请输入"
                      size="mini"
                      v-model="payInfo.documentList[scope.$index].cbdDocumentIdentifier"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="开票日期">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentDate'"
                    :rules="[{ required: true, message: '请选择开票日期', trigger: 'change' }]"
                  >
                    <el-date-picker
                      :disabled="info.documentNoDisable"
                      placeholder="选择日期"
                      size="mini"
                      type="date"
                      value-format="yyyy-MM-dd"
                      v-model="payInfo.documentList[scope.$index].cbdDocumentDate"
                    ></el-date-picker>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="税价合计">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentTotalAmount'"
                    :rules="[
                      { required: true, message: '请输入税价合计', trigger: 'blur' },
                      { required: true, pattern: /^\d{1,11}(\.(\d){1,2})?$/, message: '请输入最多11位整数，保留两位小数' }
                    ]"
                  >
                    <el-input
                      v-model="payInfo.documentList[scope.$index].cbdDocumentTotalAmount"
                      :disabled="info.documentNoDisable"
                      clearable
                      maxlength="32"
                      placeholder="请输入"
                      size="mini"
                      type="text"
                      @clear="calculationTaxCount"
                      @input="calculationTax(scope.$index)"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="税率">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentRate'"
                    :rules="[
                      { required: true, message: '请输入税率', trigger: 'blur' },
                      { required: true, pattern: /^\d{1,2}?%$/, message: '请输入最多2位整数' }
                    ]"
                  >
                    <el-input
                      v-model="payInfo.documentList[scope.$index].cbdDocumentRate"
                      :disabled="info.documentNoDisable"
                      clearable
                      maxlength="7"
                      placeholder="请输入"
                      size="mini"
                      type="text"
                      @input="cbdDocumentRateChange(scope.$index)"
                    ></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="不含税金额">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentNoTaxAmount'"
                    :rules="[
                      { required: true, message: '请输入不含税金额', trigger: 'blur' },
                      { required: true, pattern: /^\d{1,11}(\.(\d){1,2})?$/, message: '请输入最多11位整数，保留两位小数' }
                    ]"
                  >
                    <el-input
                      @input="countTaxAmount(scope.$index)"
                      clearable
                      :disabled="true"
                      maxlength="32"
                      placeholder="请输入"
                      size="mini"
                      type="text"
                      v-model="payInfo.documentList[scope.$index].cbdDocumentNoTaxAmount"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="税额">
                <template slot-scope="scope">
                  <el-form-item
                    :prop="'documentList.' + scope.$index + '.cbdDocumentTaxAmount'"
                    :rules="[
                      { required: true, message: '请输入税额', trigger: 'blur' },
                      { required: true, pattern: /^\d{1,11}(\.(\d){1,2})?$/, message: '请输入最多11位整数，保留两位小数' }
                    ]"
                  >
                    <el-input
                      @input="countTaxAmount(scope.$index)"
                      clearable
                      :disabled="true"
                      maxlength="32"
                      placeholder="请输入"
                      size="mini"
                      type="text"
                      v-model="payInfo.documentList[scope.$index].cbdDocumentTaxAmount"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column align="center" label="附件" width="200">
                <template slot-scope="scope">
                  <el-button @click="delDocumentList(scope.$index)" size="mini" type="danger" v-show="info.uploadFileShow">删除</el-button>
                  <el-button @click="openUploadDialog(scope.row)" size="mini" type="primary" v-show="info.uploadFileShow">附件上传</el-button>
                  <el-button @click="viewFile(scope.row)" size="mini" type="primary" v-show="!info.uploadFileShow">查看附件</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </div>
        <div v-show="billItemTableShow">
          <div class="view-dialog-table-title">结算单</div>
          <el-table :data="billItemTable.data" border height="400">
            <el-table-column width="55" show-overflow-tooltip align="center" type="index" label="序号"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="conNo" label="委托书编号" width="180"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="wcdSttime" label="装货日期" width="180"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="startPlace" label="装货地点" width="200"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="endPlace" label="收获地点" width="200"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="goodsName" label="货名"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="trkNo" label="车号"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="allWeight" label="吨数"></el-table-column>
            <el-table-colunm
              show-overflow-tooltip
              align="center"
              prop="ctrNo"
              label="箱号"
              v-if="info.tagName != undefined && info.tagName == 'sea'"
            ></el-table-colunm>
            <el-table-colunm
              show-overflow-tooltip
              align="center"
              prop="sealNo"
              label="铅封号"
              v-if="info.tagName != undefined && info.tagName == 'sea'"
            ></el-table-colunm>
            <el-table-column show-overflow-tooltip align="center" prop="transportQuantity" label="费用信息">
              <el-table-column show-overflow-tooltip align="center" prop="price" label="运输单价(元/吨)" width="150"></el-table-column>
              <el-table-column show-overflow-tooltip align="center" prop="allWeight" label="总重(吨)"></el-table-column>
              <el-table-column show-overflow-tooltip align="center" prop="allPrice" label="总费用" width="150"></el-table-column>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" plain @click="info.show = false">关 闭</el-button>
        <el-button type="primary" size="mini" @click="confirm" v-if="info.btnShow">{{ info.btn }}</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="uploadDialog.title"
      :visible.sync="uploadDialog.show"
      width="40%"
      style="padding:0;"
      @close="uploadDialogClose"
      v-el-drag-dialog
      v-dialogDrag
    >
      <div>
        <el-form ref="uploadFileFrom" :model="uploadFile" :rules="uploadFileRules">
          <el-row>
            <el-col>
              <el-form-item label="文件名称：" prop="name">
                <el-input v-model="uploadFile.name" style="width:80%" clearable maxlength="32"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="文件：" prop="file">
                <el-upload
                  class="upload-demo"
                  accept=".pdf, .png, .img, .jpeg, .jpg"
                  drag
                  action
                  limit="1"
                  :on-remove="removeFile"
                  :file-list="uploadFile.fileList"
                  :on-change="addFile"
                  multiple
                  :show-file-list="false"
                  :auto-upload="false"
                  :on-exceed="fileUploadExceed"
                >
                  <i class="el-icon-upload"></i>
                  <div class="el-upload__text">
                    将文件拖到此处，或
                    <em>点击上传</em>
                  </div>
                  <div class="el-upload__tip" slot="tip">只能上传pdf/图片文件,最多不超过5MB</div>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" plain @click="uploadDialog.show = false">关 闭</el-button>
        <el-button type="primary" size="mini" @click="doUploadFile">保存</el-button>
      </span>
    </el-dialog>

    <!-- 附件查看dialog -->
    <el-dialog
      :title="viewFileDialog.title"
      :visible.sync="viewFileDialog.show"
      style="padding:0;"
      :width="viewFileDialog.width"
      :height="viewFileDialog.height"
      @close="viewFileDialogClose"
      @opened="viewFileDialogOpened"
      v-el-drag-dialog
      v-dialogDrag
    >
      <img :src="payInfo.filePath" alt="附件图片" ref="viewFileImgRef" v-if="showImg" />
      <div v-if="!showImg">
        <pdf v-for="i in pdfPage.total" :key="i" :page="i" :src="payInfo.filePath" ref="pdf" :class="i < pdfPage.total ? 'pdfS' : ''"></pdf>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" plain @click="viewFileDialog.show = false">关 闭</el-button>
      </span>
    </el-dialog>

    <fileUpload :info="fileUploadData" :is-upload="fileUploadData.isUpload" @downFile="downFile"></fileUpload>
  </div>
</template>

<script>
import pdf from 'vue-pdf'
import fileUpload from './fileUpload'
import { confirmationResults, downBillsFile, platformConfirm, queryBillItem, upBillDocument } from '@/request/api'
import { accAdd, accDiv, accMul, uuid } from '@/utils/utils'
import { queryBillItemList } from '@/request/cstManagement/expenseSettlement'

export default {
  data() {
    return {
      // 文件路径
      filePaths: [],
      documentList: [],
      billItemTableShow: true,
      billId: '',
      billIdList: [],
      confirmBtn: '',
      showImg: true,
      uploadFileRules: {
        name: [{ required: true, message: '文件名称不能为空', trigger: 'change' }]
      },
      payInfo: {
        customer: '',
        billNo: '',
        billName: '',
        billTotal: '',
        bidRemark: '',
        cbDocumentNo: '',
        filePath: '',
        documentCode: '',
        documentIdentifier: '',
        documentDate: '',
        documentNoTaxAmount: '',
        documentTaxAmount: '',
        documentTotalAmount: '',
        taxCount: 0,
        documentList: [] // 发票列表
      },
      uploadFile: {
        name: '',
        fileList: []
      },
      billItemTable: {
        data: []
      },
      uploadDialog: {
        title: '',
        show: false
      },
      viewFileDialog: {
        title: '',
        show: false,
        width: 0,
        height: 0
      },
      pdfPage: {
        index: 0,
        total: 0
      },
      fileUploadData: {
        show: false,
        title: '',
        isUpload: false,
        raw: {}
      }
    }
  },
  props: {
    info: {
      type: Object,
      required: true
    }
  },
  components: {
    pdf,
    fileUpload
  },
  methods: {
    initBillItem() {
      queryBillItem({
        billId: this.billId
      }).then(res => {
        this.billItemTable.data = res.data
      })
    },
    initBillItemList() {
      queryBillItemList(this.billIdList).then(res => {
        this.billItemTable.data = res.data
        let total = 0
        for (let item of this.billItemTable.data) {
          total += Number(item.allPrice)
        }
        this.payInfo.billTotal = total
      })
    },
    downFile(id) {
      downBillsFile({
        id
      }).then(res => {
        const fileName = decodeURI(res.headers['filename'])
        if (res.headers['fileexists']) {
          this.$message.error('文件不存在')
          return
        }
        const reader = new FileReader()
        reader.readAsDataURL(new Blob([res.data]))
        reader.onload = e => {
          let a = document.createElement('a')
          a.download = fileName
          a.href = e.target.result
          document.body.appendChild(a)
          a.click()
          document.body.removeChild(a)
        }
        window.parent._$store.dispatch('app/loading_status', false)
        this.$message.success('请等待浏览器响应')
      })
    },
    // 确定
    confirm() {
      if (this.info.title === '向客户发起结算') {
        if (this.payInfo.documentList.length === 0) {
          this.$message.error('至少添加一个发票')
          return
        }
        this.$refs.payInfoRef.validate(v => {
          if (v) {
            this.$confirm('是否确认发起收款，发起收款后，发票信息和附件无法修改, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
              .then(() => {
                let data = new FormData()
                const toUpDomain = []
                for (let doc of this.payInfo.documentList) {
                  const uid = uuid()
                  if (doc.fileList) {
                    doc.fileList.forEach(f => {
                      toUpDomain.push({
                        uid,
                        cbdDocumentNo: doc.cbdDocumentNo,
                        cbdDocumentCode: doc.cbdDocumentCode,
                        cbdDocumentIdentifier: doc.cbdDocumentIdentifier,
                        cbdDocumentDate: doc.cbdDocumentDate,
                        cbdDocumentNoTaxAmount: doc.cbdDocumentNoTaxAmount,
                        cbdDocumentTaxAmount: doc.cbdDocumentTaxAmount,
                        cbdDocumentTotalAmount: doc.cbdDocumentTotalAmount,
                        cbdDocumentRate: doc.cbdDocumentRate.replace('%', '')
                      })
                      data.append('files', f.raw)
                    })
                  } else {
                    this.$message.error(`编号 ${doc.cbdDocumentNo} 的发票没有上传文件`)
                    return
                  }
                }
                // 添加新的结算单
                platformConfirm(
                  Object.assign(
                    {
                      cbId: this.info.currentData.id
                    },
                    this.payInfo
                  )
                ).then(res => {
                  toUpDomain.forEach(t => {
                    t.cbdCbId = res.data
                  })
                  data.append('domain', JSON.stringify(toUpDomain))
                  upBillDocument(data).then(() => {
                    this.$message.success('发起成功')
                    this.info.show = false
                  })
                })
              })
              .catch(() => {})
          }
        })
      } else if (!this.info.documentNoDisable) {
        try {
          if (this.payInfo.documentList.length === 0) {
            this.$message.error('至少添加一个发票')
            return
          }
          this.$refs.payInfoRef.validate(v => {
            if (v) {
              this.$confirm('是否确认发起收款，发起收款后，发票信息和附件无法修改, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                let data = new FormData()
                const toUpDomain = []
                for (let doc of this.payInfo.documentList) {
                  const uid = uuid()
                  toUpDomain.push({
                    uid,
                    cbdCbId: this.billId,
                    cbdDocumentNo: doc.cbdDocumentNo,
                    cbdDocumentCode: doc.cbdDocumentCode,
                    cbdDocumentIdentifier: doc.cbdDocumentIdentifier,
                    cbdDocumentDate: doc.cbdDocumentDate,
                    cbdDocumentNoTaxAmount: doc.cbdDocumentNoTaxAmount,
                    cbdDocumentTaxAmount: doc.cbdDocumentTaxAmount,
                    cbdDocumentTotalAmount: doc.cbdDocumentTotalAmount,
                    cbdDocumentRate: doc.cbdDocumentRate.replace('%', '')
                  })
                  if (doc.fileList) {
                    doc.fileList.forEach(file => {
                      data.append('files', new File([file], `${uid}!@#$%^&*${file.name}`))
                    })
                  } else {
                    this.$message.error(`编号 ${doc.cbdDocumentNo} 的发票没有上传附件`)
                    return
                  }
                }
                data.append(
                  'domain',
                  JSON.stringify({
                    ids: this.billIdList,
                    data: toUpDomain
                  })
                )
                upBillDocument(data).then(() => {
                  // 确认 修改状态
                  this.$message.success({
                    message: '发起成功',
                    showClose: true
                  })
                  this.info.show = false
                })
              })
            } else {
              this.$message.error('请输入必填项')
            }
          })
        } catch (e) {
          console.log(e)
        }
      } else {
        // 确认 修改状态
        confirmationResults({
          cbId: this.info.currentData.id,
          status: this.info.currentData.interimStatus
        }).then(() => {
          this.$message.success('成功')
          this.info.show = false
        })
      }
    },
    // dialog打开回调
    dialogOpen() {
      if (this.info) {
        if (this.info.billItemTableShow != undefined) {
          this.billItemTableShow = this.info.billItemTableShow
        } else {
          this.billItemTableShow = true
        }
        // 结算单发起页面 批量
        if (this.info.list && this.info.list.length) {
          this.billIdList = this.info.list.map(item => {
            return item.id
          })
          this.initBillItemList()
        } else {
          this.billId = this.info.currentData.id
          this.initBillItem()
        }
        // 判断是否禁用所有输入框
        if (this.info.documentNoDisable == undefined) {
          this.info.documentNoDisable = true
        }
        if (!('uploadFileShow' in this.info)) {
          this.info.uploadFileShow = false
        }
        if (this.info.currentData.interimStatus != 2 && this.info.title !== '向客户发起结算') {
          Object.assign(
            this.payInfo,
            {
              customer: this.info.currentData.payName,
              billNo: this.info.currentData.code,
              billName: this.info.currentData.name,
              billTotal: this.info.currentData.totalPrice,
              bidRemark: this.info.currentData.remark,
              cbDocumentNo: this.info.currentData.documentNo,
              filePath: this.info.currentData.documentFilePath,
              documentList: this.info.currentData.documents
            },
            this.info.currentData
          )
          // 计算价税合计
          let taxCount = 0
          ;(this.info.currentData.documents || []).forEach(item => {
            taxCount = accAdd(taxCount, item.cbdDocumentTotalAmount)
          })
          this.payInfo.taxCount = taxCount
        } else if (this.info.title === '向客户发起结算') {
          Object.assign(this.payInfo, {
            customer: this.info.currentData.anotherPay,
            billNo: this.info.currentData.code,
            billName: this.info.currentData.name,
            billTotal: this.info.currentData.totalPrice,
            bidRemark: this.info.currentData.remark,
            cbDocumentNo: undefined,
            filePath: undefined,
            documentCode: '',
            documentIdentifier: '',
            documentDate: '',
            documentNoTaxAmount: undefined,
            documentTaxAmount: undefined,
            documentTotalAmount: undefined
          })
        } else {
          Object.assign(this.payInfo, {
            customer: this.info.currentData.payName,
            billNo: this.info.currentData.code,
            billName: this.info.currentData.name,
            billTotal: this.info.currentData.totalPrice,
            bidRemark: this.info.currentData.remark,
            cbDocumentNo: this.info.currentData.documentNo,
            filePath: this.info.currentData.documentFilePath,
            documentCode: '',
            documentIdentifier: '',
            documentDate: '',
            documentNoTaxAmount: undefined,
            documentTaxAmount: undefined,
            documentTotalAmount: undefined,
            documentList: this.info.currentData.documents,
            list: this.info.list || []
          })
        }
      } else {
        this.$message.error('请选择一项')
      }
    },
    dialogClose() {
      this.info.billItemTableShow = true
      this.payInfo.documentList = []
      // this.$refs.payInfoRef.resetFields()
      this.$emit('dialogClose')
    },
    // 打开附件上传dialog
    openUploadDialog(raw) {
      this.fileUploadData.raw = raw
      this.fileUploadData.isUpload = true
      this.fileUploadData.show = true
      this.fileUploadData.title = '附件上传'
    },
    // 上传文件
    doUploadFile() {
      this.$refs.uploadFileFrom.validate(v => {
        if (v) {
          if (this.uploadFile.fileList.length == 0) {
            this.$message.error('请至少选择一个文件')
            return
          }
          let data = new FormData()
          data.append('file', this.uploadFile.fileList[0])
          data.append('type', 'bill')
          upBillDocument({
            id: this.billId,
            file: data
          }).then(res => {
            this.payInfo.filePath = res.msg
            this.$message.success('上传成功')
            this.uploadDialog.show = false
          })
        }
      })
    },
    // 查看上传的文件
    viewFile(raw) {
      // path 没有被组件转换
      this.fileUploadData.isUpload = false
      this.fileUploadData.show = true
      this.fileUploadData.title = '附件'
      this.fileUploadData.raw = raw
    },
    viewFileDialogOpened() {
      if (typeof this.payInfo.filePath == 'string' && !this.payInfo.filePath.endsWith('.pdf')) {
        const w = window.screen * 0.8
        this.viewFileDialog.width = this.$refs.viewFileImgRef.width + 40 > w ? w : this.$refs.viewFileImgRef.width + 'px'
      }
    },
    addFile(val) {
      this.uploadFile.fileList.push(val.raw)
      this.uploadFile.name = val.name
    },
    removeFile() {
      this.uploadFile.name = ''
      this.uploadFile.fileList = []
    },
    uploadDialogClose() {
      this.$refs.uploadFileFrom.resetFields()
      this.removeFile()
    },
    viewFileDialogClose() {
      // 重置宽度
      this.viewFileDialog.width = window.width * 0.5 + 'px'
    },
    // 覆盖文件
    fileUploadExceed(val) {
      this.uploadFile.name = val[0].name
      this.uploadFile.fileList = val
    },
    addDocumentList() {
      this.payInfo.documentList.push({
        cbdDocumentNo: '',
        cbdDocumentCode: '',
        cbdDocumentIdentifier: this.info.currentData.documentIdentifier || '',
        cbdDocumentDate: '',
        cbdDocumentNoTaxAmount: '',
        cbdDocumentTaxAmount: '',
        cbdDocumentTotalAmount: '',
        cbdDocumentRate: ''
      })
    },
    delDocumentList(row) {
      this.payInfo.documentList.splice(row, 1)
      this.calculationTaxCount()
    },
    countTaxAmount(row) {
      // const documentNoTaxAmount = parseFloat(this.payInfo.documentList[row].cbdDocumentNoTaxAmount)
      // const documentTaxAmount = parseFloat(this.payInfo.documentList[row].cbdDocumentTaxAmount)
      // this.payInfo.documentList[row].cbdDocumentTotalAmount = accAdd(documentNoTaxAmount || 0, documentTaxAmount || 0).toString()
    },
    cbdDocumentRateChange(row) {
      // if (this.payInfo.documentList[row].cbdDocumentRate) {
      //   const total = parseFloat(this.info.currentData.totalPrice)
      //   const rate = parseInt(this.payInfo.documentList[row].cbdDocumentRate)
      //   //税额=含税总价-(含税总价/1+税率)
      //   this.payInfo.documentList[row].cbdDocumentTaxAmount = (Math.round(accSub(total, accDiv(total, 1 + rate / 100)) * 100) / 100 || 0).toString()
      //   this.payInfo.documentList[row].cbdDocumentNoTaxAmount = (accSub(total, this.payInfo.documentList[row].cbdDocumentTaxAmount) || 0).toString()
      //   this.countTaxAmount(row)
      // }
      this.payInfo.documentList[row].cbdDocumentRate = this.payInfo.documentList[row].cbdDocumentRate.replaceAll('%', '') + '%'
      this.calculationTax(row)
    },
    // 计算税价合计
    calculationTax(row) {
      const taxTotal = parseFloat(this.payInfo.documentList[row].cbdDocumentTotalAmount)
      const taxRate = parseFloat(this.payInfo.documentList[row].cbdDocumentRate.replaceAll('%', '')) / 100
      if (taxTotal) {
        this.calculationTaxCount()
      }
      if (taxTotal && taxRate) {
        const taxNoAmount = accDiv(taxTotal, 1 + taxRate)
        // 不含税金额
        this.payInfo.documentList[row].cbdDocumentNoTaxAmount = (Math.round(taxNoAmount * 100) / 100 || 0).toString()
        // 含税金额
        this.payInfo.documentList[row].cbdDocumentTaxAmount = (Math.round(accMul(taxNoAmount, taxRate) * 100) / 100 || 0).toString()
      }
    },
    // 计算明细总价
    calculationTaxCount() {
      this.payInfo.taxCount = 0
      new Promise(resolve => resolve()).then(() =>
        this.payInfo.documentList.forEach(val => {
          this.payInfo.taxCount = accAdd(this.payInfo.taxCount, parseFloat(val.cbdDocumentTotalAmount) || 0)
        })
      )
    }
  }
}
</script>

<style scoped lang="scss">
.view-form {
  border: 1px solid rgb(144, 144, 144);
  border-radius: 5px;
  padding-left: 0;
}

.view-dialog-table-title {
  font-size: 13px;
  padding: 10px 0;
}

.view-dialog-table-view-btn {
  padding-bottom: 10px;
}

.pdfS {
  margin-bottom: 10px;
}
</style>
